
package com.example.app2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.example.app2.viewpager.Ad;

public class MainActivity2 extends AppCompatActivity implements View.OnFocusChangeListener, DatePickerDialog.OnDateSetListener, View.OnClickListener {

    private EditText from;
    private EditText to;
    private Button date;
    private SharedPreferences u;
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ViewPager viewPager = findViewById(R.id.viewpager);
        Ad ad = new Ad(MainActivity2.this);
        viewPager.setAdapter(ad);
        from = findViewById(R.id.from);
        to = findViewById(R.id.to);
        date = findViewById(R.id.date);
        from.setOnFocusChangeListener(this);
        to.setOnFocusChangeListener(this);
        date.setOnClickListener(this);
        //存储数据
        u = getSharedPreferences("u", 0);
        edit = u.edit();
        String from2 = u.getString("from", null);
        String to2 = u.getString("to", null);
        if (from2 != null){
            from.setText(from2);
        }
        if(to2 != null){
            to.setText(to2);
        }
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(from.getWindowToken(),0);
        return super.onTouchEvent(event);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        switch (v.getId()){
            case R.id.from:

                edit.putString("from", String.valueOf(from.getText()));
                edit.putString("to", String.valueOf(to.getText()));
                edit.commit();
                break;
            case R.id.to:
                edit.putString("from", String.valueOf(from.getText()));
                edit.putString("to", String.valueOf(to.getText()));
                edit.commit();
                break;
        }
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        date.setText(year+"年"+month+"月"+dayOfMonth+"日");
    }

    @Override
    public void onClick(View v) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, this,2024,6,18);
        datePickerDialog.show();
    }
}